<?php
namespace app\api\controller;

class Area extends Base
{
    protected $city_arr = array();
    public function _initialize()
    {
        parent::_initialize();
        //$this->chkToken();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://gd.m.utecbj.com/static/js/jqcity/allcity.js');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Content-type: application/json;charset='utf-8'"
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        $data = json_decode(str_replace(');', '', str_replace('callback(', '', $result)),true);
        $this->city_arr = $data;
    }
    public function provinces(){
        $data = $this->city_arr['province'];
        $return['result'] = array();
        foreach ($data as $key=>$val){
            $return['result'][$val['name']] = $val['id'];
        }
        $return['success'] = true;
        $return['desc'] = '';
        $this->ajaxReturn($return);
    }
    public function cities(){
        if(!isset($this->request['id'])){
            $return['success'] = false;
            $return['desc'] = '没有传入省级分类';
            $this->ajaxReturn($return);
        }
        $data = $this->city_arr['city'][$this->request['id']];
        $return['result'] = array();
        foreach ($data as $key=>$val){
            $return['result'][$val['name']] = $val['id'];
        }
        $return['success'] = true;
        $return['desc'] = '';
        $this->ajaxReturn($return);
    }
    public function getCounty(){
        if(!isset($this->request['id'])){
            $return['success'] = false;
            $return['desc'] = '没有传入城市分类';
            $this->ajaxReturn($return);
        }
        if(!isset($this->city_arr['district'][$this->request['id']])){
            $return['result']['市辖区'] = $this->request['id'];
            $return['success'] = true;
            $return['desc'] = '';
            $this->ajaxReturn($return);
        }
        $data = $this->city_arr['district'][$this->request['id']];
        $return['result'] = array();
        foreach ($data as $key=>$val){
            $return['result'][$val['name']] = $val['id'];
        }
        $return['success'] = true;
        $return['desc'] = '';
        $this->ajaxReturn($return);
    }
}